package com.cqyc.imview;

import android.content.Context;
import android.util.Log;

import com.cqyc.imview.model.OfflinePushParamBean;
import com.cqyc.network.Response;
import com.cqyc.network.getid.GetMyInformationBox;
import com.cqyc.network.http.HttpUtil;
import com.cqyc.network.http.NConfig;
import com.google.gson.Gson;
import com.tencent.qcloud.tim.tuiofflinepush.PrivateConstants;
import com.tencent.qcloud.tim.tuiofflinepush.TUIOfflinePushManager;
import com.tencent.qcloud.tuicore.TUILogin;
import com.tencent.qcloud.tuicore.interfaces.TUICallback;
import com.tencent.qcloud.tuicore.interfaces.TUILoginListener;
import com.tencent.qcloud.tuikit.TUICommonDefine;
import com.tencent.qcloud.tuikit.tuicallengine.TUICallDefine;
import com.tencent.qcloud.tuikit.tuicallkit.TUICallKit;
import com.tencent.qcloud.tuikit.tuichat.config.TUIChatConfigs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;

public class TUICallKitUtils {
    //7089 我是龙 http://ant.qiniu.dev.yctourism.com/uploads/20220223/1c1047af508e40117c70e06c39ef6a9a.jpeg?imageView2/1/w/200/h/200/interlace/1
    //7041 吴丽霞 http://ant.im.yctourism.com/FjuC9hajLIrQ7YcCSgaTbb3xQBSv?imageView2/1/w/200/h/200/interlace/1

    private static final String TAG = "TUICallKitUtils";

    //拼接内容
    public static String getTUIUserId(String userId) {
        return NConfig.Companion.getSpContent() + userId;
    }

    public static String getOtherTUIUserId(String userId) {
        return NConfig.Companion.getOtherSpContent() + userId;
    }

    public static void init(Context context, TUILoginListener listener) {
        //设置对登录结果的监听器
        TUILogin.addLoginListener(listener);
//        new TUILoginListener() {
//            @Override
//            public void onKickedOffline() {
//                super.onKickedOffline();
//                Log.i(TAG, "你被踢出局了。请重新登录！");
//            }
//
//            @Override
//            public void onUserSigExpired() {
//                super.onUserSigExpired();
//                Log.i(TAG, "您的用户签名信息已过期");
//            }
//        }
        TUICallKit.createInstance(context).enableFloatWindow(true);
        //开启消息已读回执
        TUIChatConfigs.getConfigs().getGeneralConfig().setShowRead(true);
    }

    public static boolean isLogined() {
        return TUILogin.isUserLogined();
    }

    public static void logo(Context context, String userID, boolean isTest, String userSig) {
        if (!isLogined()) {
            //登录
            int appId = isTest ? 1400734360 : 1400780192;
//            userSig = GenerateTestUserSig.genTestUserSig(getTUIUserId(userID));
            Log.e("im登录", "Source:" + getTUIUserId(userID) + "  appId:" + appId + "  userSig:" + userSig);

            TUILogin.login(context.getApplicationContext(),
                    appId,     // 请替换为步骤一取到的 SDKAppID
                    getTUIUserId(userID),   // 请替换为您的 UserID
                    userSig,      // 您可以在控制台中计算一个 UserSig 并填在这个位置
                    new TUICallback() {
                        @Override
                        public void onSuccess() {
                            Log.i(TAG, "登录成功");
                            if ("merchant".equals(GetMyInformationBox.INSTANCE.getSource())){
                                Log.i(TAG, "注册离线推送");

                                OfflinePushParamBean offlinePushParamBean = new OfflinePushParamBean();
                                offlinePushParamBean.setHuaweiPushBussinessId("27804");
                                offlinePushParamBean.setHuaweiBadgeClassName("com.cqyc.shangjia.activity.StartActivity");

                                offlinePushParamBean.setVivoPushBussinessId("27875");
                                offlinePushParamBean.setHuaweiBadgeClassName("com.cqyc.shangjia.activity.StartActivity");

                                offlinePushParamBean.setXiaomiPushBussinessId("27879");
                                offlinePushParamBean.setXiaomiPushAppId("2882303761520220898");
                                offlinePushParamBean.setXiaomiPushAppKey("5972022042898");

                                String json = new Gson().toJson(offlinePushParamBean);
                                TUIOfflinePushManager.getInstance().registerPush(json , context);
                            }
                        }

                        @Override
                        public void onError(int errorCode, String errorMessage) {
                            Log.e(TAG, "登录失败: " + errorCode + " msg:" + errorMessage);
                        }
                    });
        }
    }

    //b0071f1840ffe29d5c09a5daa4e2745495cbee5dfe6b041995346842b594bcc3
    public static void logo(Context context, String userID, boolean isTest, String userSig, String name, String face, OnImLogCallBack callBack) {
        if (!isLogined()) {
            //登录
            int appId = isTest ? 1400734360 : 1400780192;
//            userSig = GenerateTestUserSig.genTestUserSig(getTUIUserId(userID));
            Log.e("im登录", "Source:" + getTUIUserId(userID) + "  appId:" + appId + "  userSig:" + userSig);
            TUILogin.login(context.getApplicationContext(),
                    appId,     // 请替换为步骤一取到的 SDKAppID
                    getTUIUserId(userID),   // 请替换为您的 UserID
                    userSig,      // 您可以在控制台中计算一个 UserSig 并填在这个位置
                    new TUICallback() {
                        @Override
                        public void onSuccess() {
                            Log.i(TAG, "登录成功");
                            setSelfInformation(context, name, face);
                            if (callBack != null) {
                                callBack.logCallBack(true, "");
                            }
                        }

                        @Override
                        public void onError(int errorCode, String errorMessage) {
                            Log.e(TAG, "登录失败: " + errorCode + " msg:" + errorMessage);
                            if (callBack != null) {
                                callBack.logCallBack(false, errorCode + " msg:" + errorMessage);
                            }
                        }
                    });
        }
    }

    public static void setSelfInformation(Context context, String nickName, String faceUrl) {
        //设置自己的头像和名字，向对方发起时对方会显示该内容
        //用户昵称不能超过500字节，用户头像必须是 URL 格式
        TUICallKit.createInstance(context).setSelfInfo(nickName, faceUrl, new TUICommonDefine.Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    public static void logOut() {
        if (!isLogined()) return;
        TUILogin.logout(new TUICallback() {
            @Override
            public void onSuccess() {
                Log.i(TAG, "退出登录成功");
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                Log.i(TAG, "退出登录失败 code:" + errorCode + "  ---  " + errorMessage);
            }
        });
    }

    public interface OnImLogCallBack {
        void logCallBack(boolean isSuccess, String msg);
    }
}


